package _60_Jackson;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

public class jsonNode {

    @Test
    public void testJsonNode() throws IOException {
//        FileReader fileReader = new FileReader("./jackson_test.json");
        String studentStr = "{\n" +
                "  \"id\":\"09194567\",\n" +
                "  \"studentName\":\"王三\",\n" +
                "  \"nickName\": null,\n" +
                "  \"englishName\":\"King Three\",\n" +
                "  \"age\":32,\n" +
                "  \"email\":\"123@qq.com\",\n" +
                "  \"position\":[12.34, 56.78],\n" +
                "  \"valid\":true\n" +
                "}";

        ObjectMapper mapper = new ObjectMapper();
        JsonNode rootNode = mapper.readTree(studentStr);
        System.out.println(rootNode.toString());


    }

    @Test
    public void testObject() {
//        Object js = "{\n" +
//                "    \"位置\": [12.34, 56.78]\n" +
//                "}";
//        System.out.println(js.getClass());
//        ObjectNode jsonNode = new ObjectNode();
//        ArrayNode d = (ArrayNode) obj;
//        System.out.println(d.get(0));
//        System.out.println(d.get(1));

        ArrayList<Number> arrayList = new ArrayList<>();
        arrayList.add(12.34);
        arrayList.add(12);

        double latitude = Double.parseDouble(arrayList.get(1).toString());
        double longitude = Double.parseDouble(arrayList.get(0).toString());
        System.out.println(latitude);
        System.out.println(longitude);
    }

    @Test
    public void ttt() {
        String dynamicTemplate = "{\n" +
                "  \"template\" : \"*-lenet-index\",\n" +
                "  \"mappings\" : {\n" +
                "      \"_default_\" : {\n" +
                "        \"dynamic_templates\" : [ {\n" +
                "          \"date_fields\" : {\n" +
                "            \"mapping\" : {\n" +
                "              \"type\" : \"date\",\n" +
//            "              \"copy_to\": \"catch_all\",\n" +
                "              \"format\": \"strict_date_optional_time\",\n" +
                "              \"fields\" : {\n" +
                "                \"raw\" : {\n" +
                "                  \"ignore_above\" : 256,\n" +
                "                  \"index\" : true,\n" +
                "                  \"type\" : \"keyword\"\n" +
                "                },\n" +
                "                \"default_analyzer\" : {\n" +
                "                  \"index\" : true,\n" +
                "                  \"type\" : \"text\",\n" +
                "                  \"index_options\": \"offsets\",\n" +
                "                  \"term_vector\": \"with_positions_offsets\"\n" +
                "                }\n" +
                "              }\n" +
                "            },\n" +
                "            \"match_mapping_type\" : \"date\", \n" +
                "            \"unmatch\": \"*_name\" \n" +
                "          }\n" +
                "        }, {\n" +
                "          \"long_fields\" : {\n" +
                "            \"mapping\" : {\n" +
                "              \"type\" : \"long\",\n" +
//            "              \"copy_to\": \"catch_all\",\n" +
                "              \"fields\" : {\n" +
                "                \"raw\" : {\n" +
                "                  \"ignore_above\" : 256,\n" +
                "                  \"index\" : true,\n" +
                "                  \"type\" : \"keyword\"\n" +
                "                },\n" +
                "                \"default_analyzer\" : {\n" +
                "                  \"index\" : true,\n" +
                "                  \"type\" : \"text\",\n" +
                "                  \"index_options\": \"offsets\",\n" +
                "                  \"term_vector\": \"with_positions_offsets\"\n" +
                "                }\n" +
                "              }\n" +
                "            },\n" +
                "            \"match_mapping_type\" : \"long\"\n" +
                "          }\n" +
                "        }, {\n" +
                "          \"double_fields\" : {\n" +
                "            \"mapping\" : {\n" +
                "              \"type\" : \"double\",\n" +
//            "              \"copy_to\": \"catch_all\",\n" +
                "              \"fields\" : {\n" +
                "                \"raw\" : {\n" +
                "                  \"ignore_above\" : 256,\n" +
                "                  \"index\" : true,\n" +
                "                  \"type\" : \"keyword\"\n" +
                "                },\n" +
                "                \"default_analyzer\" : {\n" +
                "                  \"index\" : true,\n" +
                "                  \"type\" : \"text\",\n" +
                "                  \"index_options\": \"offsets\",\n" +
                "                  \"term_vector\": \"with_positions_offsets\"\n" +
                "                }\n" +
                "              }\n" +
                "            },\n" +
                "            \"match_mapping_type\" : \"double\"\n" +
                "          }\n" +
                "        }, {\n" +
                "          \"message_field\" : {\n" +
                "            \"mapping\" : {\n" +
                "              \"fielddata\" : false,\n" +
                "              \"index\" : true,\n" +
                "              \"omit_norms\" : true,\n" +
//            "              \"copy_to\": \"catch_all\",\n" +
                "              \"type\" : \"text\"\n" +
                "            },\n" +
                "            \"match_mapping_type\" : \"string\",\n" +
                "            \"match\" : \"message\"\n" +
                "          }\n" +
                "        }, {\n" +
                "          \"string_fields\" : {\n" +
                "            \"mapping\" : {\n" +
                "              \"fielddata\" : false,\n" +
                "              \"index\" : true,\n" +
                "              \"analyzer\": \"ik_smart\",\n" +
                "              \"omit_norms\" : true,\n" +
                "              \"type\" : \"text\",\n" +
//            "              \"copy_to\": \"catch_all\",\n" +
                "              \"index_options\": \"offsets\",\n" +
                "              \"term_vector\": \"with_positions_offsets\",\n" +
                "              \"fields\" : {\n" +
                "                \"raw\" : {\n" +
                "                  \"ignore_above\" : 256,\n" +
                "                  \"index\" : true,\n" +
                "                  \"type\" : \"keyword\"\n" +
                "                },\n" +
                "                \"default_analyzer\" : {\n" +
                "                  \"analyzer\": \"custom_standard\",\n" +
                "                  \"index\" : true,\n" +
                "                  \"type\" : \"text\",\n" +
                "                  \"index_options\": \"offsets\",\n" +
                "                  \"term_vector\": \"with_positions_offsets\"\n" +
                "                }\n" +
                "              }\n" +
                "            },\n" +
                "            \"match_mapping_type\" : \"string\",\n" +
                "            \"match\" : \"*\"\n" +
                "          }\n" +
                "        } ],\n" +
                "        \"properties\" : {\n" +
//            "          \"catch_all\": {\n" +
//            "            \"omit_norms\": true,\n" +
//            "            \"type\": \"text\"\n" +
//            "          },\n" +
                "          \"@timestamp\" : {\n" +
                "            \"type\" : \"date\"\n" +
                "          },\n" +
                "          \"_adjInfo._name\" : {\n" +
                "            \"type\" : \"text\"\n" +
                "          },\n" +
                "          \"@version\" : {\n" +
                "            \"index\" : true,\n" +
                "            \"type\" : \"keyword\"\n" +
                "          }\n" +
                "        }\n" +
                "      }\n" +
                "    },\n" +
                "  \"aliases\" : { }\n" +
                "}";
        System.out.println(dynamicTemplate.toString());
    }

}
